#include "CEnemy.h"
#include "CPlayer.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "ObjectManager.h"
#include "ObjectFactory.h"
#include "CExpOrb.h"
#include "CBaseAIState.h"
#include "CEventSystem.h"
#include "CMessageSystem.h"
#include "CCamera.h"
#include "CAnimationEngine.h"
#include "CWorldEngine.h"
#include "ComboSystem.h"

CEnemy::CEnemy()
{
	SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/stickEnemy.png", D3DCOLOR_XRGB(255, 255, 255)));
	SetWidth(32);
	SetHeight(64);
	m_nType = OBJ_ENEMY;
	CEventSystem::GetInstance()->RegisterListeners("MegaDeath", this );

}
CEnemy::~CEnemy()
{
	for(unsigned int i = 0; i < m_vAIStates.size(); ++i)
		delete m_vAIStates[i];

	CEventSystem::GetInstance()->UnregisterListeners("MegaDeath", this );
}

void CEnemy::Update(float fElapsedTime)
{
	if(m_vAIStates.size())
		m_vAIStates[m_nCurrentAI]->Update(fElapsedTime);

	CBase::Update(fElapsedTime);
	if( CWorldEngine::GetInstance()->CheckCollision( this ) != SOLID && CWorldEngine::GetInstance()->CheckCollision( this ) != WATER )
	{
		SetVelY( m_enemyPhysics.IsFalling( GetVelY(), fElapsedTime ) );

	}
	else
	{
		SetVelY( 0 );
		CPhysics::GetInstance()->SetIsJumping( false );
	}

}
bool CEnemy::CheckCollision( CBase* pBase )
{
	if( pBase == this )
		return false;

	if( pBase->GetType() == OBJ_ENEMY )
		return false;

	if( pBase->GetType() == OBJ_PLAYER )
	{
		RECT empty;
		if( IntersectRect( &empty, &((CPlayer*)pBase)->GetAttackRect() , &this->GetCollisionRect() ) )
		{
			for( unsigned int i = 0 ; i < ((CPlayer*)pBase)->GetWeapon().size() ; ++i  )
			{
				if( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetWeaponType() == ((CPlayer*)pBase)->GetCurrentWeapon() )
				{
					switch( ((CPlayer*)pBase)->GetEnumCurrentWeapon() )
					{
					case 0:
						{
							if( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetWeaponLevel() == ((CPlayer*)pBase)->GetGauntletLevel() )
							{
								int nComboDamage = 0; 
								if( ComboSystem::GetInstance()->GetLastButton() == 'z' || ComboSystem::GetInstance()->GetLastButton() == 'Z' ) //CSGD_DirectInput::GetInstance()->KeyReleased( DIK_Z ) ) // || CSGD_DirectInput::GetInstance()->CheckKeys() == 'Z' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetLightBaseDamage() * ((CPlayer*)pBase)->GetGauntletLevel())+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'x' || ComboSystem::GetInstance()->GetLastButton() == 'X' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetMediumBaseDamage() * ((CPlayer*)pBase)->GetGauntletLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;						
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'c' || ComboSystem::GetInstance()->GetLastButton() == 'C' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage()  )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetHeavyBaseDamage() * ((CPlayer*)pBase)->GetGauntletLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
							}
							break;
						}
					case 1:
						{
							if( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetWeaponLevel() == ((CPlayer*)pBase)->GetSpearLevel() )
							{
								int nComboDamage = 0; 
								if( ComboSystem::GetInstance()->GetLastButton() == 'z' || ComboSystem::GetInstance()->GetLastButton() == 'Z' ) //CSGD_DirectInput::GetInstance()->KeyReleased( DIK_Z ) ) // || CSGD_DirectInput::GetInstance()->CheckKeys() == 'Z' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetLightBaseDamage() * ((CPlayer*)pBase)->GetSpearLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'x' || ComboSystem::GetInstance()->GetLastButton() == 'X' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetMediumBaseDamage() *((CPlayer*)pBase)->GetSpearLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;						
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'c' || ComboSystem::GetInstance()->GetLastButton() == 'C' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetHeavyBaseDamage() *((CPlayer*)pBase)->GetSpearLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
							}
							break;
						}
					case 2:
						{
							if( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetWeaponLevel() == ((CPlayer*)pBase)->GetSwordLevel() )
							{
								int nComboDamage = 0; 
								if( ComboSystem::GetInstance()->GetLastButton() == 'z' || ComboSystem::GetInstance()->GetLastButton() == 'Z' ) //CSGD_DirectInput::GetInstance()->KeyReleased( DIK_Z ) ) // || CSGD_DirectInput::GetInstance()->CheckKeys() == 'Z' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetLightBaseDamage() *((CPlayer*)pBase)->GetSwordLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'x' || ComboSystem::GetInstance()->GetLastButton() == 'X' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetMediumBaseDamage() * ((CPlayer*)pBase)->GetSwordLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;						
								}
								if( ComboSystem::GetInstance()->GetLastButton() == 'c' || ComboSystem::GetInstance()->GetLastButton() == 'C' )
								{
									if( ComboSystem::GetInstance()->GetComboDamage() )
									{
										nComboDamage = ComboSystem::GetInstance()->GetComboDamage();
										ComboSystem::GetInstance()->SetComboDamage( 0 );
									}
									SetHP( GetHP() - ( ( ((CPlayer*)pBase)->GetWeapon()[ i ]->GetHeavyBaseDamage() * ((CPlayer*)pBase)->GetSwordLevel() )+ nComboDamage ) );
									ComboSystem::GetInstance()->SetLastButton( ' ' );
									if( GetHP() <= 0 )
									{
										CExpOrb* temp_orb = new CExpOrb();
										temp_orb->SetPosX(GetPosX()+GetWidth()/2);
										temp_orb->SetPosY(GetPosY()+GetHeight());
										temp_orb->SetPlayer((CPlayer*)pBase);
										ObjectManager::GetInstance()->AddObject(temp_orb);
										temp_orb->Release();
										ObjectManager::GetInstance()->RemoveObject( this );
									}
									return true;
								}
							}
							break;
						}
					};
				}
			}
		}
	}
	return false;
}
RECT CEnemy::GetCollisionRect()
{
	RECT enemyRect;
	enemyRect.left = ( long )GetPosX();
	enemyRect.top = ( long )GetPosY();
	enemyRect.right =	( long )( GetPosX() + GetWidth() );
	enemyRect.bottom =	( long )( enemyRect.top + GetHeight() );

	return enemyRect;
}
void CEnemy::SetCurrentAI(int nAI_ID)
{
	for(unsigned int i = 0; i < m_vAIStates.size(); ++i)
	{
		if(nAI_ID == m_vAIStates[i]->GetAIType())
		{
			m_nCurrentAI = i;
			break;
		}
	}
}
void CEnemy::HandleEvents( CEvents* pEvent )
{
	if( pEvent->GetEvents() == "MegaDeath" )
	{
		CMessageSystem::GetInstance()->SendMsg( new CDestroyEnemyMessage( this ) );
	}
}
void CEnemy::Render()
{
	if(GetFacingRight())
		CSGD_TextureManager::GetInstance()->Draw(GetImageID(), (int)GetPosX()- CCamera::GetInstance()->GetCameraPosX(), (int)GetPosY()- CCamera::GetInstance()->GetCameraPosY());
	else
		CSGD_TextureManager::GetInstance()->Draw(GetImageID(), ((int)GetPosX() + GetWidth()) - CCamera::GetInstance()->GetCameraPosX(), (int)GetPosY()- CCamera::GetInstance()->GetCameraPosY(), -1.0f);

	if(m_vAIStates.size())
		m_vAIStates[m_nCurrentAI]->Render();

	//char buffer[ 20 ];
	//sprintf_s( buffer,strlen( buffer ), "HP: %d ",GetHP() );
	//CSGD_Direct3D::GetInstance()->DrawText( buffer, GetPosX(), GetPosY() - 20, 255,0 , 0 );
}